package com.frxuexi.park2209_2.video.view

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.setMargins
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.bean.SearchBean
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209_2.R
import com.frxuexi.park2209_2.databinding.ActivityVideoSearchBinding
import com.frxuexi.park2209_2.video.adapter.VideoAdapter
import com.frxuexi.park2209_2.video.viewmodel.VideoViewModel
import com.google.android.flexbox.FlexboxLayout
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/VideoSearchActivity")
@AndroidEntryPoint
class VideoSearchActivity : BaseActivity<ActivityVideoSearchBinding, VideoViewModel>() {
    val videoAdapter by lazy { VideoAdapter() }
    override fun initData() {
        vm.searchSuc.observe(this) {
            videoAdapter.apply {
                addData(it)
                notifyDataSetChanged()
            }
        }
        vm.addSearchSuc.observe(this) {
            ToastUtils.showLong(it.toString())
        }

        vm.deleteSuc.observe(this){
            ToastUtils.showLong("删除成功")
        }

        vm.getSearchSuc.observe(this){search->
            binding.flex.removeAllViews()
            for (i in 0..search.size-1){
                /*val tv = TextView(this)
                tv.apply {
                    setBackgroundResource(R.drawable.shape_tv)
                    setText(it.get(i).key)
                    setPadding(15,5,15,5)
                    layoutParams=FlexboxLayout.LayoutParams(
                        FlexboxLayout.LayoutParams.WRAP_CONTENT,
                        FlexboxLayout.LayoutParams.WRAP_CONTENT)
                        .apply { setMargins(10) }
                }*/
                val view = LayoutInflater.from(this).inflate(R.layout.flex_view, null)
                view.findViewById<TextView>(R.id.tv)
                    .apply {
                        setText(search.get(i).key)
                        setOnClickListener {
                            binding.videoSearch.setText(search.get(i).key)
                            videoAdapter.data.clear()
                            vm.findVideoByName(search.get(i).key)
                        }
                    }

                view.findViewById<ImageView>(R.id.iv).setOnClickListener {
                    binding.flex.removeView(view)
                    vm.deleteSearch(search.get(i))
                }


                binding.flex.addView(view)
            }
        }
    }

    override fun initView() {
        binding.btnSearch.setOnClickListener {
            val search = binding.videoSearch.text.toString()
            videoAdapter.data.clear()
            vm.findVideoByName(search)
            vm.addSearch(SearchBean(key = search))
            vm.getSearch()
        }

        binding.rvVideo.apply {
            adapter = videoAdapter
            layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL)
        }



        vm.getSearch()

    }

}